<?php
/**
 * 
 * 
 * $Rev: 3 $
 * $Author: thejahweh $
 * $Date: 2010-06-30 13:10:01 +0000 (Wed, 30 Jun 2010) $
 * $Id: timestamp.class.php 3 2010-06-30 13:10:01Z thejahweh $
 * $URL: http://allone.googlecode.com/svn/trunk/plugins/timestamp/timestamp.class.php $
 * @author: Adrian Liechti <info@jahweh.ch>
 * @copyright: 2010 Adrian Liechti, GPL3 and CC3 BY-SA
 * TODO berarbeiten
 */

class timeStamp {
	
	public static $times = array();
	private $timeStart;
	private $timeEnd;
	private $name;
	private $id;
	
	public function __construct ($name = '', $id = false, $start = true) {
		$this->name		= $name;
		$this->timeEnd	= 0;
		if (isset(self::$times[$this->name]) === false or is_array(self::$times[$this->name]) === false) {
			self::$times[$this->name] = array();
		}
		if ($id === false) {
			$this->id = count(self::$times[$name]);
		} else {
			$this->id = $id;
		}
		self::$times[$this->name][$this->id] = 0;
		if ($start) $this->start();
	}
	
	public function start ($id = false) {
		if ($id !== false) $this->id = $id;
		$this->timeStart = $this->microtimeFloat();
	}
	
	public function end ($round = false) {
		if ($round === false) {
			$endTime = abs($this->microtimeFloat() - $this->timeStart);
		} else {
			$endTime = abs(round($this->microtimeFloat() - $this->timeStart, $round));
		}
		$this->timeEnd += $endTime;
		return self::$times[$this->name][$this->id] = $endTime;
	}
	
	private function microtimeFloat() {
		list($usec, $sec) = explode(' ', microtime());
		return ((float)$usec + (float)$sec);
	}
	
}
?>